import {getUserLocation} from '../../../Tools/index.js';
import {getTopPics,getGroupGoodsForIndexXCX,login} from '../../../Tools/HTTPOpertion.js';
Page({
  data: {
    ptgoods:[],
    kjgoods: [],
    msgoods: [],
    banners:[],
    city:'',
    codeid:'',
    isfresh:false,
    msg:'努力加载数据中...',
    pages:["/pages/others/homePage/homePage", "/pages/active/makeGroups/makeGroups", "/pages/others/search/search", '/pages/active/bargainOwnPage/bargainOwnPage', "/pages/active/bargainHomePage/bargainHomePage", "/pages/active/miaosha/miaosha", "/pages/active/makeGroupsDetails/makeGroupsDetails","/pages/active/miaoshaDetails/miaoshaDetails"]
  },
  onLoad: function (options) {
    const that=this;
    if (!wx.getStorageSync('token')) {
      this.setData({ isfresh: true })
      login().then(res=>{
        that.Show()
      }).catch(err=>{

      })
    }else{
      this.Show();
    }
  },
  onShow:function(){
    if(this.data.isfresh&&wx.getStorageSync('token')){
      this.Show();
      this.setData({isfresh:false})
    }
  },
  Show: function () {
    const self=this;
    let city= wx.getStorageSync('city');
    if(city){
       this.getData(city)
    }else{
      getUserLocation('main',function(){
        city= wx.getStorageSync('city')
        self.getData(city)
      });
    }
  },
  // 加载banner
  loadBanners:function(){
     var self = this;
      getTopPics({}).then(function (result) {
        self.setData({
            banners: result.data.topPics
        });
      });
  },
  getData:function(city){
    const self=this;
    var codeid = wx.getStorageSync('codeid');
    self.setData({
      city:city,
      codeid:codeid,
    })
    self.loadOpenedGroup();
  },
  // banner跳转链接
  link:function(e){
    let type = e.currentTarget.dataset.info.activity_type,id=e.currentTarget.dataset.info.activity_id,url='';
    // 0:h5链接 1:拼团 2:砍价 3:秒杀'
    switch (type) {
      case 0:
         url="/pages/others/learnOurs/learnOurs"
        break;
      case 1:
         url=`/pages/active/makeGroupsDetails/makeGroupsDetails?id=${id}`
         break;
      case 2:
         url= `/pages/active/BargainDetails/BargainDetails?id=${id}&from=banner`
         break;
      case 3:
         url=`/pages/active/miaoshaDetails/miaoshaDetails?id=${id}`
         break;
      default:

    }
    wx.navigateTo({url: url})
  },
// 导航跳转地址
  toNextPage:function(e){
    const url=e.currentTarget.dataset.id?this.data.pages[e.currentTarget.dataset.page]+'?id='+e.currentTarget.dataset.id:this.data.pages[e.currentTarget.dataset.page];
    wx.navigateTo({url: url})
  },
  // 城市选择
  choiseCity:function(){
    wx.navigateTo({url:"/pages/others/choiseCity/choiseCity?city="+this.data.city})
  },
  loadOpenedGroup:function(){
    var self = this;
    // 根据城市获取商品
    var codeid = wx.getStorageSync("codeid");
    getGroupGoodsForIndexXCX({areaId:codeid}).then(function (res) {
      self.loadBanners();
      if(res.code == 0){
        self.setData({
          ptgoods:res.data.listOne,
          kjgoods:res.data.listThree,
          msgoods:res.data.listTwo,
          msg:'商品正在准备中，敬请期待'
        });
      }else{
          wx.showToast({
              title: res.message||'网络错误，稍后再试',
              icon: 'none'
          })
      }
    })
  },
  // 一键授权
  setuserinfo:function(e){
    this.Show()
  },
  toSearch:function(){
    wx.navigateTo({
      url: "/pages/others/searchUser/searchUser"
    })
  }
})
